package com.bawei.week3.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.MediaStore;

import com.bawei.week3.entity.ContactsEntity;
import com.bawei.week3.entity.MusicEntity;
import com.bawei.week3.entity.PictureEntity;

import java.util.ArrayList;
import java.util.List;

public class ContentUtils {
    public static List<ContactsEntity> getContacts(Context context){
        ArrayList<ContactsEntity> contactsEntities = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        if(cursor != null){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                contactsEntities.add(new ContactsEntity(name,number));
            }
        }
        return contactsEntities;

    }

    //返回本地歌曲全部名称
    public static List<MusicEntity> getSongs(Context context){
        ArrayList<MusicEntity> songBeans = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(cursor != null ){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String album_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                //根据专辑id获得专辑封面
                Cursor query = contentResolver.query(Uri.parse("content://media/external/audio/albums/" + album_id), null, null, null, null);
                if(query != null){
                    while (query.moveToNext()){
                        String album = query.getString(query.getColumnIndex("album_art"));
                        songBeans.add(new MusicEntity(name,singer,data,album));
                    }
                    query.close();//关闭游标
                }

            }
            cursor.close();//关闭游标
        }
        return songBeans;
    }


    public static List<PictureEntity> getPictures(Context context){
        ArrayList<PictureEntity> contactsEntities = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if(cursor != null){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                contactsEntities.add(new PictureEntity(name,data));
            }
        }
        return contactsEntities;

    }









}
